package tech.yangzhijie.login.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.yangzhijie.login.pojo.TUser;
import tech.yangzhijie.login.service.GroupService;
import tech.yangzhijie.login.service.UserService;
import tech.yangzhijie.login.vo.GroupVo;
import tech.yangzhijie.login.vo.UserVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private GroupService groupService;
	
	@RequestMapping("login")
	public String toLogin(){
		return "user/login";
	}
	
	@RequestMapping("/main")
	public String login(TUser user, HttpSession session, Model model){
		UserVo vo = userService.login(user);
		if(vo != null){
			session.setAttribute("user", vo);
			return "main/main";
		}
		return "user/login";
	}
	
	@RequestMapping("/saveUser")
	@ResponseBody
	public Object saveUser(UserVo userVo){
		userService.saveUser(userVo);
		return null;
	}
	
	@RequestMapping("checkUsername")
	@ResponseBody
	public Map<String ,Boolean> checkUsername(String username){
		Map<String ,Boolean> map = new HashMap<>();
		UserVo vo = userService.checkUsername(username);
		if(vo==null){
			map.put("flag", true);
		}else{
			map.put("flag", false);
		}
		return map;
	}
	
	@RequestMapping("/toUser")
	public String toUser(HttpServletRequest request){
		List<GroupVo> list = groupService.findGroupList();
		request.setAttribute("list", list);
		return "user/register";
	}
}
